/*
 * axios封装
 */
import axios from 'axios'
import QS from 'qs'
import {message} from 'ant-design-vue';
 
// 请求超时时间
axios.defaults.timeout = 30000;
 
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
 
// 响应拦截器
axios.interceptors.response.use(    
    response => {    
        if (response.status === 200) {
            let result  = response.data
            if(result.errorCode == 0){
                return Promise.resolve(result.data);
            }else{
                console.error(result.message)
                message.error(result.message)
                return Promise.reject(result)
            }
        }  
    },
    // 服务器状态码不是200的情况    
    error => {        
        if (error.response.status) {
            message.error("网络异常")
            console.error(error)
            return Promise.reject(error)
        } 
    }
);
/** 
 * get方法，对应get请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function get(url, params){    
    return new Promise((resolve, reject) =>{        
        axios.get(url, {            
            params: params        
        })        
        .then(data => {            
            resolve(data)
        })
        .catch(err => {            
            reject(err.data)        
        })
    });
}
/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function post(url, params) {    
    return new Promise((resolve, reject) => {
        axios.post(url, params,{
            headers: {'Content-Type':'application/json;charset=UTF-8' }
        })
        .then(data => {            
            resolve(data);        
        })        
        .catch(err => {            
            reject(err.data)        
        })
    });
}

export function postFormData(url, params) {    
    return new Promise((resolve, reject) => {
        axios.post(url, QS.stringify(params))
        .then(data => {            
            resolve(data);        
        })        
        .catch(err => {            
            reject(err.data)
        })
    });
}

export function upload(url, formData) {
    return new Promise((resolve, reject) => {
        let configs = {
            headers:{'Content-Type':'multipart/form-data'}
        }
        axios.post(url, formData,configs)
        .then(data => {            
            resolve(data);        
        })        
        .catch(err => {            
            reject(err.data)        
        })
    });
}



